/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SaleManagerServer.DAO;

import SaleManagerServer.Pojo.PositionPOJO;
import SaleManagerServer.Util.Constants;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;

/**
 *
 * @author BPT
 */
public class PositionDAO {
    private static PositionDAO instance;

    private PositionDAO() {
    }

    public static PositionDAO getInstance() {
        if (instance == null) {
            instance = new PositionDAO();
        }
        return instance;
    }
    
    public List<PositionPOJO> getPositionInfoByCustomerID(String userId){
        List<PositionPOJO> result = new ArrayList<PositionPOJO>();
        Connection conn = null;
        PreparedStatement prepareStatement=null;
        String query = Constants.BLANK_VALUE;
        ResultSet rs = null;
        PositionPOJO positionPOJO;
        try {
            query = "Select * from Positions where User_Id=? ORDER BY time DESC LIMIT 0,20";
            conn = SaleManagerServer.Util.Connection.getConnection();
            if (conn != null) {
                prepareStatement = conn.prepareStatement(query);
                prepareStatement.setString(1, userId);
            } else {
                return result;
            }
            prepareStatement.execute();
            rs = prepareStatement.getResultSet();
            while (rs.next()) {
                positionPOJO = new PositionPOJO();
                // Set
                positionPOJO.setUserId(rs.getInt("user_id"));
                positionPOJO.setLatitude(rs.getDouble("Latitude"));
                positionPOJO.setLongitude(rs.getDouble("Longtitude"));
                positionPOJO.setDatetime(rs.getDate("time"));

                // Add book on list
                result.add(positionPOJO);
            }

        } catch (Exception ex) {
//            LogSystem.getInstance().info(
//                    "BookDAO.getAllBook:" + ex);
        } finally {
            try {
                rs.close();
                prepareStatement.close();
                conn.close();
            } catch (Exception ex) {
//                LogSystem.getInstance().info(
//                        "BookDAO.getAllBook: " + sqlEx);
            }
//            LogSystem.getInstance().info(
//                    "BookDAO.getAllBook:End");
        }
        return result;
    }
    
    public int insertPosition(PositionPOJO positionPOJO){
        Connection conn = null;
        PreparedStatement prepareStatement=null;
        String query = Constants.BLANK_VALUE;
        try {
            query = "Insert into Positions values(null,?,?,?,?)";
            conn = SaleManagerServer.Util.Connection.getConnection();
            if (conn != null) {
                prepareStatement = conn.prepareStatement(query);
                prepareStatement.setInt(1, positionPOJO.getUserId());
                prepareStatement.setDouble(2, positionPOJO.getLatitude());
                prepareStatement.setDouble(3, positionPOJO.getLongitude());
                prepareStatement.setString(4, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(positionPOJO.getDatetime()));
            } else {
                return -1;
            }
            if(prepareStatement.execute()){
                return 1;
            }else{
                return 0;
            }

        } catch (Exception ex) {
//            LogSystem.getInstance().info(
//                    "BookDAO.getAllBook:" + ex);
            return -1;
        } finally {
            try {
                prepareStatement.close();
                conn.close();
            } catch (Exception ex) {
//                LogSystem.getInstance().info(
//                        "BookDAO.getAllBook: " + sqlEx);
                return -1;
            }
//            LogSystem.getInstance().info(
//                    "BookDAO.getAllBook:End");
        }
    }
    
}
